package org.geohunter.activity.osmmap;

import org.andnav.osm.ResourceProxy;
import org.andnav.osm.util.GeoPoint;
import org.andnav.osm.views.OpenStreetMapView;
import org.andnav.osm.views.OpenStreetMapView.OpenStreetMapViewProjection;
import org.andnav.osm.views.overlay.OpenStreetMapViewOverlay;
import org.geohunter.activity.map.DensityOverlayDelegate;

import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Point;

public class OsmDensityOverlay extends OpenStreetMapViewOverlay {
    private final DensityOverlayDelegate mDelegate;

    public OsmDensityOverlay(ResourceProxy resourceProxy,
            DensityOverlayDelegate densityOverlayDelegate) {
        super(resourceProxy);
        mDelegate = densityOverlayDelegate;
    }

    private Point mScreenTopLeft = new Point();
    private Point mScreenBottomRight = new Point();
    
    @Override
    protected void onDraw(final Canvas canvas, final OpenStreetMapView osmView) {
        final OpenStreetMapViewProjection projection = osmView.getProjection();
        final GeoPoint newGeoTopLeft = projection.fromPixels(0, 0);
        final GeoPoint newGeoBottomRight = projection.fromPixels(osmView.getWidth(), 
                osmView.getHeight());

        projection.toMapPixels(newGeoTopLeft, mScreenTopLeft);
        projection.toMapPixels(newGeoBottomRight, mScreenBottomRight);

        Matrix matrix = canvas.getMatrix();
        canvas.setMatrix(new Matrix());
        mDelegate.draw(canvas, 
                newGeoTopLeft.getLatitudeE6(), newGeoTopLeft.getLongitudeE6(),
                newGeoBottomRight.getLatitudeE6(), newGeoBottomRight.getLongitudeE6(),
                mScreenTopLeft, mScreenBottomRight);
        canvas.setMatrix(matrix);
    }

    @Override
    protected void onDrawFinished(Canvas c, OpenStreetMapView osmv) {
    }
}
